package com.ruoyi.system.ali;

import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.ruoyi.common.enums.ChannelEnum;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.common.utils.file.AliOss;
import com.ruoyi.common.utils.sign.SignMD5Util;
import com.ruoyi.system.biz.CashBizService;
import com.ruoyi.system.domain.Bank;
import com.ruoyi.system.domain.CashHistory;
import com.ruoyi.system.domain.Order;
import com.ruoyi.system.domain.UserRate;
import com.ruoyi.system.dto.ApiByPhoneAddDto;
import com.ruoyi.system.service.BankService;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.UserRateService;
import com.ruoyi.system.vo.ApiPayResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.TreeMap;

@Component
@Slf4j
public class TbPayService implements ChannelService {

    @Resource
    private ISysConfigService configService;
    @Resource
    private BankService bankService;


    @Resource
    private CashBizService cashService;

    @Resource
    private UserRateService userRateService;

    @Override
    public String getType() {
        return ChannelEnum.TB.getCode();
    }

    public Object create(AliPayDto aliPayDto, Order order) {
        Bank bank = bankService.getRand(order.getAmount(), this.getType(), order.getAmount());
        return create(order, bank);
    }

    @Override
    public Object createByPhone(ApiByPhoneAddDto apiByPhoneAddDto, Order order) {
        Bank bank = bankService.getUserBank(order.getUserId(), order.getAmount(), this.getType());
        return create(order, bank);
    }


    private Object create(Order order, Bank bank) {
        order.setAmount(BigDecimalUtil.setDecimal(order.getAmount()));

        //冻结收款用户的金额
        CashHistory userCashHis = new CashHistory(bank.getUserId(), order.getAmount(), "reduce", "代收订单", "freeze");
        userCashHis.setOrderNo(order.getOrderNo());
        cashService.updateCash(bank.getUserId(), userCashHis);
        order.setChannelCode(this.getType());
        order.setChannelName(ChannelEnum.getName(this.getType()));
        order.setAccount(bank.getCardNo());
        order.setName(bank.getName());
        order.setType("hand");
        order.setUserId(bank.getUserId());
        UserRate userRate = userRateService.checkUserRate(bank.getUserId(), this.getType());
        order.setEarning(order.getAmount().multiply(userRate.getRate().divide(new BigDecimal(100))));
        order.setUsername(bank.getUsername());
        order.setZfbUserId(bank.getZfbUserId());


        ApiPayResultVo apiVo = new ApiPayResultVo();
        apiVo.setAmount(String.valueOf(order.getAmount()));
        apiVo.setOrderNo(order.getOrderNo());
        apiVo.setMerchantId(order.getMerchantId());
        TreeMap<String, String> map = SignMD5Util.objToMap(apiVo);
        map.put("payType", "5");
        String sign = SignMD5Util.getSign(map, "sign");
        String payUrl = configService.selectConfigByKey("template");
        payUrl = payUrl + "?" + SignMD5Util.getStr(map, sign);
        apiVo.setUrl(payUrl);

        try {
            //将链接解析成二维码
            String url = bank.getCardNo();
            QrConfig qrConfig = new QrConfig();
            qrConfig.setWidth(200);
            qrConfig.setHeight(200);
            byte[] bytes = QrCodeUtil.generatePng(url, qrConfig);
            String projectName = configService.selectConfigByKey("projectName");
            String picUrl = AliOss.uploadToOss(bytes, projectName + "/orderReceive/" + apiVo.getOrderNo() + ".jpg");
            order.setZfbUrl(url);
            order.setHtml(picUrl);
            order.setPic(picUrl);
        } catch (Exception e) {
            log.info("淘宝咸鱼转二维码失败:{}", e.getMessage());
        }

        return apiVo;
    }


}
